//该文件的作用：解决react-router v6不能在组件外路由跳转的问题
import { createHashHistory } from "history";
import { PropsWithChildren } from "react";
import React from "react";
import { Router } from "react-router-dom";

export const history = createHashHistory();

interface HistoryRouterProps {
	history: typeof history;
}

export const HistoryRouter: React.FC<PropsWithChildren<HistoryRouterProps>> = ({ history, children }) => {
	const [state, setState] = React.useState({
		action: history.action,
		location: history.location,
	});
	React.useLayoutEffect(() => {
		history.listen(setState);
	}, [history]);
	return React.createElement(Router, Object.assign({ children, navigator: history }, state));
};
